home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 October / PCWorld_2006-10_cd.bin / v cisle / pdffc / ps2pdf995.exe / pdf995 / res / convert / ps2epsi.ps < prev    next >
Text File  |  2003-12-13  |  9KB  |  250 lines

  1. %    Copyright (C) 1990, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: ps2epsi.ps,v 1.9.2.1 2003/12/12 22:07:59 giles Exp $
  14. % Convert an arbitrary PostScript file to an EPSI file.
  15. %
  16. % Please do not contact these users if you have questions.  They no longer
  17. % have the time, interest, or current expertise to keep this code working.
  18. % If you find bugs, please send proposed fixes to bug-gs@ghostscript.com.
  19. %
  20. % Bug fix 2002-04-20 by rayjj: Bounding box was incorrect since it depended
  21. %   on the dither pattern and gray shade at the boundary. Changed to use
  22. %   8-bit grayscale preview image to allow correct bounding box (at the
  23. %   expense of a 8x larger preview image). Also moved .setsafe until after
  24. %   the device and file operations are complete (but still before the input
  25. %   file is processed.
  26. % Bug fix 2000-04-11 by lpd: if a font didn't have a FontName (which is the
  27. %   case for bitmap fonts produced by recent versions of dvips), setfont
  28. %   caused an error.
  29. % Bug fix 8/21/99 by lpd: many of the margin and width computations were
  30. %   wrong (off by 1).  The code only "worked" because the bugs were
  31. %   (mostly) in conservative directions.
  32. % Modified 3/17/98 by lpd to make it possible to run this file without
  33. %   running the ps2epsi script first, for debugging.
  34. % Bug fix 9/29/97 by lpd <ghost@aladdin.com>: if the page size wasn't an
  35. %   exact multiple of 8 bits, an incorrect bounding box (or a rangecheck
  36. %   error) could occur.
  37. % Patched 7/26/95 by
  38. %    Greg P. Kochanski <gpk@bell-labs.com>
  39. %   to add many new DSC comments and make the comments conforming.
  40. % Original version contributed by
  41. %    George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  42. %
  43.  
  44. % Initialize, and redefine copypage and showpage.
  45.  
  46. % ps2edict is normally defined in the pre-loaded code created by the
  47. % ps2epsi script.
  48. /ps2edict where { pop } { /ps2edict 25 dict def } ifelse
  49. ps2edict begin
  50.  
  51.                 % The main procedure
  52.   /ps2epsi
  53.    {                % Open the file
  54.      outfile (w) file /epsifile exch def
  55.                     % Get the device parameters
  56.      currentdevice getdeviceprops .dicttomark 
  57.      /HWSize get aload pop
  58.        /devheight exch def
  59.        /devwidth exch def
  60.      matrix defaultmatrix
  61.        /devmatrix exch def
  62.                 % Make a corresponding 8-bit deep memory device
  63.      devmatrix devwidth devheight
  64.      256 string 0 1 255 { 1 index exch dup 255 exch sub put } for
  65.      makeimagedevice
  66.      /arraydevice exch def
  67.      arraydevice
  68.      % Turn on anti-aliasing
  69.      mark /TextAlphaBits 4 /GraphicsAlphaBits 4 6 -1 roll
  70.      putdeviceprops
  71.      setdevice    % (does an erasepage)
  72.      /rowwidth devwidth def
  73.      /row rowwidth string def
  74.      /zerorow rowwidth string def    % all zero
  75.                 % Replace the definition of showpage
  76.      userdict /showpage { ps2edict begin epsipage end } bind put
  77.      userdict /setfont { ps2edict begin epsisetfont end } bind put
  78.      //systemdict /.setsafe known { .setsafe } if
  79.    } bind def
  80.  
  81.  /epsifontdict 100 dict def
  82.  
  83.  /epsisetfont
  84.  {
  85.  % code here keeps a list of font names in dictionary epsifontdict
  86.  /tmpfont exch def
  87.  tmpfont /FontName known {
  88.    /tmpfontname tmpfont /FontName get def
  89.    epsifontdict tmpfontname known not { epsifontdict tmpfontname 0 put } if
  90.    epsifontdict tmpfontname 2 copy get 1 add put
  91.  } if
  92.  tmpfont setfont
  93.  } bind def
  94.  
  95. % Get a scan line from the memory device, zeroing any bits beyond
  96. % the device width.
  97. /getscanline {        % <device> <y> <string> getscanline <string>
  98.   dup 4 1 roll copyscanlines pop
  99.   16#ff00 devwidth 7 and neg bitshift 255 and
  100.   dup 0 ne {
  101.     1 index dup length 1 sub 2 copy get 4 -1 roll and put
  102.   } {
  103.     pop
  104.   } ifelse
  105. } bind def
  106.  
  107. /margintest {        % <y-start> <step> <y-limit> margintest <y-non-blank>
  108.             % <y-start> <step> <y-limit> margintest -
  109.   { dup arraydevice exch row getscanline
  110.     zerorow ne { exit } if pop
  111.   } for
  112. } bind def
  113.  
  114.  
  115.   /epsiNameStr 200 string def
  116.   /epsiNpages 0 def
  117.   /epsiNpageStr 20 string def
  118.   /epsipage
  119.    {      
  120.      /epsiNpages epsiNpages 1 add def
  121.      /loopcount devheight 1 sub def
  122.  
  123.      % Find top margin -- minimum Y of non-blank scan line.
  124.      -1 0 1 loopcount margintest
  125.      dup -1 eq { (blank page!!\n) print quit }{ exch pop } ifelse 
  126.      /tm exch def
  127.  
  128.      % Find bottom margin -- maximum Y of non-blank scan line.
  129.      loopcount -1 0 margintest
  130.      /bm exch def
  131.      
  132.      % Initialise limit variables
  133.      /loopcount rowwidth 1 sub def
  134.      /lm loopcount def 
  135.      /rm 0 def 
  136.  
  137.      % Find left and right boundaries of image
  138.      tm 1 bm
  139.       { % Get more data
  140.     arraydevice exch row getscanline pop
  141.     % Scan from left to find first non-zero element
  142.     % We save first the element, then the index
  143.     -1 0 1 loopcount
  144.     { dup row exch get 0 ne { exch pop exit }{ pop } ifelse
  145.     } for
  146.     % If we found -1, row is blank ..
  147.     dup -1 ne 
  148.     { % Find the leftmost index
  149.           dup lm lt
  150.           % If the new index is less, we save index and element
  151.           { /lm exch def } { pop } ifelse
  152.       % Now find the rightmost index
  153.       loopcount -1 0
  154.           { dup row exch get 0 ne { exit }{ pop } ifelse
  155.           } for
  156.       dup rm gt
  157.           % If the new index is greater, we save index and element
  158.           { /rm exch def } { pop } ifelse
  159.     } if
  160.       } for
  161.  
  162.      % Calculate the bounding box values.
  163.      % Note that these must be corrected to produce closed-open intervals.
  164.      /llx lm def
  165.      /lly devheight bm sub 1 sub def
  166.      /urx rm 1 add def
  167.      /ury devheight tm sub def
  168.  
  169.     % Write out the magic string and bounding box information
  170.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  171.      /epsititle where { pop epsifile epsititle writestring } if
  172.      /epsicreator where { pop epsifile epsicreator writestring } if
  173.      /epsicrdt where { pop epsifile epsicrdt writestring } if
  174.      /epsifor where { pop epsifile epsifor writestring } if
  175.      epsifile flushfile
  176.  
  177.     % Write out the page count:
  178.      epsifile (%%Pages: ) writestring
  179.      epsifile epsiNpages epsiNpageStr cvs writestring
  180.      epsifile (\n) writestring
  181.      epsifile flushfile
  182.  
  183.     % Write out the list of used fonts:
  184.      epsifile (%%DocumentFonts:) writestring
  185.      epsifontdict {
  186.                     epsifile ( ) writestring
  187.                     pop epsiNameStr cvs epsifile exch writestring
  188.                     } forall
  189.      epsifile (\n) writestring
  190.      epsifile flushfile
  191.  
  192.      epsifile (%%BoundingBox: ) writestring
  193.      epsifile llx write==only epsifile ( ) writestring
  194.      epsifile lly write==only epsifile ( ) writestring
  195.      epsifile urx write==only epsifile ( ) writestring
  196.      epsifile ury write==
  197.  
  198.     % Define character and bit widths for the output line buffer:
  199.      /cwidth rm lm sub 1 add def
  200.      /out cwidth string def
  201.  
  202.      epsifile (%%EndComments\n\n) writestring
  203.      epsifile (%%BeginProlog\n) writestring
  204.      epsifile (%%BeginPreview: ) writestring
  205.      epsifile cwidth write==only epsifile ( ) writestring
  206.      epsifile bm tm sub 1 add write==only epsifile ( 8 ) writestring
  207.      epsifile bm tm sub 1 add
  208.      cwidth 39 add 40 idiv mul write==
  209.      epsifile flushfile
  210.  
  211.      gsave
  212.  
  213.      tm 1 bm
  214.       { % Get a scan line interval from the array device
  215.     arraydevice exch row copyscanlines lm cwidth getinterval
  216.     % Write out the hex data as 40 bytes per line (82 chars)
  217.     0 40 cwidth
  218.      { epsifile (% ) writestring 
  219.        epsifile exch 2 index exch
  220.        dup cwidth exch sub 40 .min getinterval writehexstring
  221.        epsifile (\n) writestring
  222.      } for
  223.         pop
  224.       } for
  225.  
  226.      epsifile (%%EndImage\n) writestring
  227.      epsifile (%%EndPreview\n) writestring
  228.      epsifile flushfile
  229.      grestore
  230.      erasepage initgraphics
  231.  
  232.      DonePage 0 1 put
  233.    } bind def
  234.  
  235.  
  236. (outfile) getenv
  237.   { /outfile exch def 
  238.     ps2epsi
  239.  
  240.     /DonePage 1 string def
  241.     (%stdin) (r) file cvx execute0
  242.     DonePage 0 get 0 eq { showpage } if
  243.   } if
  244.  
  245. end
  246. quit
  247.